<template>
  <div class="login-container">

    <el-row>
      <el-col :span="16">
        <div class="text-center" style="padding:50px 50px 20px;">

          <el-carousel :interval="5000" arrow="always" height="22vw">
            <el-carousel-item v-for="(item,key) in swiperList" :key="key">
              <el-image
                style="width: 80%;"
                :src="item.src"
                fit="contain"
              />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="contanier" style="height:45vh">
          <!-- <v-chart ref="chart1" :options="orgOptions" :auto-resize="true"></v-chart> -->
          <line-chart :chart-data="lineChartData" />
        </div>

      </el-col>
      <el-col :span="8">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">Login Form</h3>
          </div>

          <el-form-item prop="userName">
            <span class="svg-container">
              <svg-icon name="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.userName"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon name="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :name="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin"
          >
            Login
          </el-button>

          <!-- <div class="tips">
            <span style="margin-right:20px;">username: test01</span>
            <span> password: 123456</span>
          </div> -->
        </el-form>

      </el-col>
    </el-row>

    <footer
      class="absolute padding text-center flex align-center justify-center text-white"
    >
      <span class="margin-right">
        Copyright © 2016-2021 JiuFang E-commerce Logistics Co. Ltd All Rights
        Reserved.
      </span>

      <a
        target="_blank"
        href="mailto:ceo@tofba.com"
        style="text-decoration:none;"
      >
        <img src="../../assets/images/ico_mailme_11.png">
      </a>
    </footer>
  </div>
</template>

<script>

import LineChart from './components/LineChart'
const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}
export default {
  name: 'Login',
  components: {
    LineChart
  },
  data() {
    return {

      lineChartData: lineChartData.newVisitis,
      loginForm: {
        userName: '',
        password: ''
      },
      loginRules: {
        userName: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      swiperList: []
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.queryList()
  },
  methods: {
    queryList() {
      const files = require.context('@/assets/images/advertise', false, /.jpg$/)
      const arr = []
      for (let i = 0; i < files.keys().length; i++) {
        arr.push({
          src: require(`@/assets/images/advertise/${files.keys()[i].substr(2, files.keys()[i].length)}`)
        })
      }
      const files1 = require.context('@/assets/images/advertise', false, /.png$/)
      const arr1 = []
      for (let i = 0; i < files1.keys().length; i++) {
        arr1.push({
          src: require(`@/assets/images/advertise/${files1.keys()[i].substr(2, files1.keys()[i].length)}`)
        })
      }

      this.swiperList = [...arr, ...arr1]
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // console.log('12')
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.password === '123456') {
            sessionStorage.setItem('isSimple', true)
          } else {
            sessionStorage.removeItem('isSimple')
          }
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              console.log('this.redirect', this.redirect)
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
              this.$store.dispatch('user/getInfo')
              this.$store.dispatch('public/getPublicData')
            })
            .catch(err => {
              this.loading = false
            })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 0px;
      height: 70px;
      color: $light_gray;
      margin: 0px auto;
      text-align: center;
      font-weight: bold;
      background: url('../../assets/images/login/logo.png') no-repeat center /
        60%;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  footer {
    width: 100%;
    bottom: 3%;
    left: 0;
  }
}
.contanier {
  position: relative;
}
</style>
